<?php
include_once "conn.php";
$type=$_GET['type'];
$price=$_GET['price'];
$time=$_GET['time'];
$search=$_GET['search'];
$goods_search = '%'.$search.'%';
$arr=[];
if ($search === '') { //search为空
    if ($type !== '' && $price !== '' && $time !== '') { //选择三种
        if ($price === '由低到高') {
            $sql = $db->query("SELECT goods_id,goods_title,goods_price,goods_url
            FROM goods WHERE goods_type = '$type' ORDER BY goods_price,goods_time DESC");
            while ($row = $sql -> fetch_assoc()) {
                array_push($arr,$row);
            }
            echo json_encode($arr);
        }else{
            $sql = $db->query("SELECT goods_id,goods_title,goods_price,goods_url
            FROM goods WHERE goods_type = '$type' ORDER BY goods_price DESC,goods_time DESC");
            while ($row = $sql -> fetch_assoc()) {
                array_push($arr,$row);
            }
            echo json_encode($arr);
        }
    }else if($type === '' && $price === '' && $time === ''){ //全不选
        $sql = $db ->query(
        'SELECT goods_id,goods_title,goods_url,goods_price FROM goods');
        while ($row = $sql -> fetch_assoc()) {
        array_push($arr,$row);
        }
        echo json_encode($arr);
    }else{ //选择两种
        if (($type!==''&&$price!=='')||($type!==''&&$time!=='')||($price!==''&&$time!=='')) {
            if ($type !== '' && $price !== '') { //选择的是价格和类型
                if ($price === '由低到高') {
                    $sql = $db->query("SELECT goods_id,goods_title,goods_price,goods_url
                    FROM goods  WHERE goods_type = '$type' ORDER BY goods_price");
                    while ($row = $sql -> fetch_assoc()) {
                        array_push($arr,$row);
                    }
                    echo json_encode($arr);
                }else{
                    $sql = $db->query("SELECT goods_id,goods_title,goods_price,goods_url
                    FROM goods  WHERE goods_type = '$type' ORDER BY goods_price DESC");
                    while ($row = $sql -> fetch_assoc()) {
                        array_push($arr,$row);
                    }
                    echo json_encode($arr);
                }
            }
            else if($type !== '' && $time !== ''){ //选择的是类型和时间
                $sql = $db->query("SELECT goods_id,goods_title,goods_price,goods_url
                FROM goods WHERE goods_type = '$type' ORDER BY goods_time DESC");
                while ($row = $sql -> fetch_assoc()) {
                    array_push($arr,$row);
                }
                echo json_encode($arr);
            }else{ //选择的是价格和时间
                if ($price === '由低到高') {
                    $sql = $db->query("SELECT goods_id,goods_title,goods_price,goods_url
                    FROM goods ORDER BY goods_price,goods_time DESC");
                    while ($row = $sql -> fetch_assoc()) {
                        array_push($arr,$row);
                    }
                    echo json_encode($arr);
                }else{
                    $sql = $db->query("SELECT goods_id,goods_title,goods_price,goods_url
                    FROM goods ORDER BY goods_price DESC,goods_time DESC");
                    while ($row = $sql -> fetch_assoc()) {
                        array_push($arr,$row);
                    }
                    echo json_encode($arr);
                }
            }
        }else{ //当只选择一个
            if ($type !== '') {
                $sql = $db->query("SELECT goods_id,goods_title,goods_price,goods_url
                FROM goods WHERE goods_type = '$type'");
                while ($row = $sql -> fetch_assoc()) {
                    array_push($arr,$row);
                  }
                echo json_encode($arr);
            }else if($price !== ''){
                if ($price === '由低到高') { //按价格由低到高
                    $sql = $db->query("SELECT goods_id,goods_title,goods_price,goods_url
                    FROM goods ORDER BY goods_price");
                    while ($row = $sql -> fetch_assoc()) {
                        array_push($arr,$row);
                    }
                    echo json_encode($arr);
                }else{ //按商品价格由高到低
                    $sql = $db->query("SELECT goods_id,goods_title,goods_price,goods_url
                    FROM goods ORDER BY goods_price DESC");
                    while ($row = $sql -> fetch_assoc()) {
                        array_push($arr,$row);
                    }
                    echo json_encode($arr);
                }
            }else{ //按最新上架排序
                $sql = $db->query("SELECT goods_id,goods_title,goods_price,goods_url
                FROM goods ORDER BY goods_time DESC");
                while ($row = $sql -> fetch_assoc()) {
                    array_push($arr,$row);
                }
                echo json_encode($arr);
            }
        }
    }
}else{ //search不为空
    if ($type !== '' && $price !== '' && $time !== '') { //选择三种
        if ($price === '由低到高') {
            $sql = $db->query("SELECT goods_id,goods_title,goods_price,goods_url
            FROM goods WHERE goods_type = '$type' AND goods_title LIKE '$goods_search'
            ORDER BY goods_price,goods_time DESC");
            while ($row = $sql -> fetch_assoc()) {
                array_push($arr,$row);
            }
            echo json_encode($arr);
        }else{
            $sql = $db->query("SELECT goods_id,goods_title,goods_price,goods_url
            FROM goods WHERE goods_type = '$type' AND goods_title LIKE '$goods_search' 
            ORDER BY goods_price DESC,goods_time DESC");
            while ($row = $sql -> fetch_assoc()) {
                array_push($arr,$row);
            }
            echo json_encode($arr);
        }
    }else if($type === '' && $price === '' && $time === ''){ //全不选
        $sql = $db ->query(
        "SELECT goods_id,goods_title,goods_url,goods_price FROM goods
         WHERE goods_title LIKE '$goods_search'");
        while ($row = $sql -> fetch_assoc()) {
        array_push($arr,$row);
        }
        echo json_encode($arr);
    }else{ //选择两种
        if (($type!==''&&$price!=='')||($type!==''&&$time!=='')||($price!==''&&$time!=='')) {
            if ($type !== '' && $price !== '') { //选择的是价格和类型
                if ($price === '由低到高') {
                    $sql = $db->query("SELECT goods_id,goods_title,goods_price,goods_url
                    FROM goods  WHERE goods_type = '$type' AND goods_title LIKE '$goods_search'
                     ORDER BY goods_price");
                    while ($row = $sql -> fetch_assoc()) {
                        array_push($arr,$row);
                    }
                    echo json_encode($arr);
                }else{
                    $sql = $db->query("SELECT goods_id,goods_title,goods_price,goods_url
                    FROM goods  WHERE goods_type = '$type' AND goods_title LIKE '$goods_search' 
                    ORDER BY goods_price DESC");
                    while ($row = $sql -> fetch_assoc()) {
                        array_push($arr,$row);
                    }
                    echo json_encode($arr);
                }
            }
            else if($type !== '' && $time !== ''){ //选择的是类型和时间
                $sql = $db->query("SELECT goods_id,goods_title,goods_price,goods_url
                FROM goods WHERE goods_type = '$type' AND goods_title LIKE '$goods_search' 
                ORDER BY goods_time DESC");
                while ($row = $sql -> fetch_assoc()) {
                    array_push($arr,$row);
                }
                echo json_encode($arr);
            }else{ //选择的是价格和时间
                if ($price === '由低到高') {
                    $sql = $db->query("SELECT goods_id,goods_title,goods_price,goods_url
                    FROM goods WHERE goods_title LIKE '$goods_search' ORDER BY goods_price,goods_time DESC");
                    while ($row = $sql -> fetch_assoc()) {
                        array_push($arr,$row);
                    }
                    echo json_encode($arr);
                }else{
                    $sql = $db->query("SELECT goods_id,goods_title,goods_price,goods_url
                    FROM goods WHERE goods_title LIKE '$goods_search'  ORDER BY goods_price DESC,goods_time DESC");
                    while ($row = $sql -> fetch_assoc()) {
                        array_push($arr,$row);
                    }
                    echo json_encode($arr);
                }
            }
        }else{ //当只选择一个
            if ($type !== '') {
                $sql = $db->query("SELECT goods_id,goods_title,goods_price,goods_url
                FROM goods WHERE goods_type = '$type' AND goods_title LIKE '$goods_search'");
                while ($row = $sql -> fetch_assoc()) {
                    array_push($arr,$row);
                  }
                echo json_encode($arr);
            }else if($price !== ''){
                if ($price === '由低到高') { //按价格由低到高
                    $sql = $db->query("SELECT goods_id,goods_title,goods_price,goods_url
                    FROM goods WHERE goods_title LIKE '$goods_search' ORDER BY goods_price");
                    while ($row = $sql -> fetch_assoc()) {
                        array_push($arr,$row);
                    }
                    echo json_encode($arr);
                }else{ //按商品价格由高到低
                    $sql = $db->query("SELECT goods_id,goods_title,goods_price,goods_url
                    FROM goods WHERE goods_title LIKE '$goods_search' ORDER BY goods_price DESC");
                    while ($row = $sql -> fetch_assoc()) {
                        array_push($arr,$row);
                    }
                    echo json_encode($arr);
                }
            }else{ //按最新上架排序
                $sql = $db->query("SELECT goods_id,goods_title,goods_price,goods_url
                FROM goods WHERE goods_title LIKE '$goods_search' ORDER BY goods_time DESC");
                while ($row = $sql -> fetch_assoc()) {
                    array_push($arr,$row);
                }
                echo json_encode($arr);
            }
        }
    }
}

$db->close();
?>